import random, json, pyperclip, os, pyautogui, win32gui, win32con
from threading import Event
from time import sleep, time
from libs import get_image_pos

class AutoCraftActions:
  def __init__(self):
      pass
  
  def start_actions(self):
    self.toggle_dunzao_table()
    self.open_duanzao_frame()
    self.check_buffs()
    self.start_craft()
    self.send_mail()

  def toggle_dunzao_table(self):
    pyautogui.press("8")
    sleep(1)
    pyautogui.press("8")
    sleep(1)
    pyautogui.press("9")
    sleep(1)
    pyautogui.press("8")
    sleep(2)
  
  def open_duanzao_frame(self):
    # 打开锻造界面
    pyautogui.press("num1")
    sleep(5)

  def start_craft(self):
    craft_pos = get_image_pos("images_2/word_craft.png")
    while craft_pos is None:
      sleep(5)
      pyautogui.press("num5")
      craft_pos = get_image_pos("images_2/word_craft.png")
  
  def check_buffs(self):
    # 检查buffs
    pyautogui.press("num9")
    sleep(5)
    dh_pos = self.get_dh_pos()
    if dh_pos:
      pyautogui.press("num7")
      sleep(10)
      pyautogui.press("num8")
      sleep(5)
    qs_pos = self.get_qs_pos()
    if qs_pos:
      pyautogui.press("num6")
      sleep(2)

  def send_mail(self):
    sleep(2)
    # 使用凯蒂玩具
    pyautogui.press("num4")
    sleep(2)
    # target 凯蒂
    pyautogui.press("num4")
    sleep(2)
    # 与凯蒂交互
    pyautogui.press("9")
    sleep(2)
    # 打开邮箱界面
    pyautogui.press("num4")
    sleep(2)
    pyautogui.press("num2")
    sleep(5)
    
  def get_qs_pos(self):
    count = 1
    pos = None
    while (pos is None) and (count < 5):
      sleep(1)
      pos = get_image_pos("images_2/word_qs.png")
      count += 1
    return pos
  
  def get_dh_pos(self):
    count = 1
    pos = None
    while (pos is None) and (count < 5):
      sleep(1)
      pos = get_image_pos("images_2/word_dh.png")
      count += 1
    return pos